﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyTienDien
{
    public partial class BienLai : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("KhachHang");
        private DataTable dtbl = new DataTable("BienLai");
        private SqlDataAdapter da = new SqlDataAdapter();
        //ham ket noi
        private bool IsConnect()
        {
            bool isOpend = false;
            string cn = "Data Source=THANHHA;Initial Catalog=QUANLYTIENDIEN;Integrated Security=True";
            try
            {
                //MessageBox.Show("ket noi thanh cong");
                con = new SqlConnection(cn);
                con.Open();//mo ket noi
                if (con.State == ConnectionState.Open)
                {
                    isOpend = true;
                }
                else
                    isOpend = false;
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể kết nối", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            return isOpend;
        }
        //ham dong ket noi
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }
        public BienLai()
        {
            InitializeComponent();
        }

        private void HienThi()
        {
            SqlCommand com = new SqlCommand();//khai bao 1 command
            com.Connection = con;//ket noi
            com.CommandType = CommandType.Text;//khai bao kieu command
            com.CommandText = "SELECT KhachHang.MACONGTO,HOTEN,SONHA,CHISOCU,CHISOMOI,SOTIEN FROM KhachHang,BienLai WHERE KhachHang.MACONGTO=BienLai.MACONGTO ";
            da.SelectCommand = com;//gan command cho data
            da.Fill(dtbl);//nap dl len table
            dtgridbienlai.DataSource = dtbl;//do dl len datagridview



        }
        //ham load DL
        private void LoadDaTa()
        {
            //viet theo kieu Bindings
            txtmact.DataBindings.Clear();//xoa DL co san trong textbox
            txtmact.DataBindings.Add("Text", dtgridbienlai.DataSource, "MACONGTO");
            //Text:mac dinh,MACONGTO:ten cot trong datagridview

            txtcu.DataBindings.Clear();
            txtcu.DataBindings.Add("Text", dtgridbienlai.DataSource, "CHISOCU");
            txtmoi.DataBindings.Clear();
            txtmoi.DataBindings.Add("Text", dtgridbienlai.DataSource, "CHISOMOI");
            txttien.DataBindings.Clear();
            txttien.DataBindings.Add("Text", dtgridbienlai.DataSource, "SOTIEN");

        }

        private void Form2_Load(object sender, EventArgs e)
        {
            IsConnect();
            HienThi();
            LoadDaTa();


        }

        //Bắt sự kiện chọn cả dòng
        private void dtgridbienlai_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                dtgridbienlai.CurrentRow.Selected = true;//DL đc chon ca dong
            }
            catch { }
        }

        
        
        private void btnTinh_Click(object sender, EventArgs e)
        {
            double socu = Convert.ToDouble(txtcu.Text); 
            double somoi=Convert.ToDouble(txtmoi.Text);
            double sotien;
            
            if ((somoi-socu) < 50)
            {
                sotien = (somoi - socu) * 1250;
                txttien.Text = sotien.ToString();
            }
            else if ((somoi - socu) >= 50 && (somoi - socu) < 100)
            {
                sotien = (somoi - socu) * 1500;
                txttien.Text = sotien.ToString();
            }
            else
            {
                sotien = (somoi - socu) * 2000;
                txttien.Text = sotien.ToString();
            }
        }
        

        private void btnSua_Click(object sender, EventArgs e)
        {
            int IsUpdated;
            if(IsConnect())
            {
                string ma = txtmact.Text;
                string cu = txtcu.Text;
                string moi = txtmoi.Text;
                string tien = txttien.Text;
                SqlCommand comUpdate = new SqlCommand();
                comUpdate.CommandText = "UPDATE BienLai SET MACONGTO="+ma+",CHISOCU="+cu+",CHISOMOI="+moi+",SOTIEN="+tien+"WHERE MACONGTO="+ma+"";
                comUpdate.Connection = con;
                comUpdate.CommandType = CommandType.Text;
                IsUpdated = comUpdate.ExecuteNonQuery();
                if (IsUpdated > 0)
                {
                    MessageBox.Show("Sửa thành công");
                }
                else
                {
                    MessageBox.Show("Chưa sửa được");
                }


            }
            else
            {
                MessageBox.Show("Không kết nối được");
            }

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int IsDeleted;
            if (IsConnect())
            {
                string ma = txtmact.Text;
                string cu = txtcu.Text;
                string moi = txtmoi.Text;
                string tien = txttien.Text;
                SqlCommand comDelete = new SqlCommand();
                comDelete.CommandText = "DELETE FROM BienLai WHERE MACONGTO="+ma+"";
                comDelete.Connection = con;
                comDelete.CommandType = CommandType.Text;
                IsDeleted = comDelete.ExecuteNonQuery();
                if (IsDeleted > 0)
                {
                    MessageBox.Show("Xoá thành công");
                }
                else
                {
                    MessageBox.Show("Chưa xoá được");
                }
            }
                else
                {
                    MessageBox.Show("Không kết nối được");
                }
            }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            if (IsConnect())
            {
                SqlCommand comUpdate = new SqlCommand();
                comUpdate.CommandText = "SELECT KhachHang.MACONGTO,HOTEN,SONHA,CHISOCU,CHISOMOI,SOTIEN FROM KhachHang,BienLai WHERE KhachHang.MACONGTO=BienLai.MACONGTO  ";
                comUpdate.Connection = con;
                comUpdate.CommandType = CommandType.Text;
                SqlDataAdapter dta = new SqlDataAdapter(comUpdate);
                DataTable dtb = new DataTable();
                dta.Fill(dtb);
                dtgridbienlai.DataSource = dtb;
                dtgridbienlai.Refresh();
                LoadDaTa();
            }
        }
        //THOÁT
        private void button4_Click(object sender, EventArgs e)
        {
            Close();//thoát
            Dispose();//giải phóng bộ nhớ
            MessageBox.Show("Bạn có chắc chắn thoát không?", "Thoát");
        }
        }
    }

